package core

import grails.converters.JSON
import grails.converters.XML

class CountryController {
	//static layout = 'main'
	/*
	 // action as method
	 // maps to /country/index or /country/
	 def index() {
	 method()
	 render text: "<h1><center>Country.index</center></h1>"
	 }
	 // action as closure
	 // maps to /country/list
	 def list = { render text: "Country.list" }
	 private method () {
	 println 'privateMethod'
	 //render text: 'privateMethod'
	 } */
	//==========setting the defaultAction to be list method==============================
	//	static defaultAction = "list"
	//	// action as method
	//	// maps to /country/index
	//	def index() {
	//	render text: "Country.index"
	//	}
	//	// action as closure
	//	// maps to /country/list
	//	def list = {
	//	render text: "Country.list"
	//	}
	//	// maps to /country/show
	//	def show() {
	//	render text: "Country.show"
	//	}


	//==================

	/*def index() {
	 render text: """Country.index
	 <pre>
	 servletContext: ${servletContext['servletContextKey']}
	 session: ${session.sessionKey}
	 request: ${request.requestKey}
	 params: ${params}
	 flash: ${flash}
	 ${link(action:'list', 'Go to list')}
	 </pre>
	 """
	 }
	 def list = {
	 servletContext['servletContextKey'] = 'servletContext Value'
	 session['sessionKey'] = 'session Value'
	 request['requestKey'] = 'request Value'
	 flash['flashKey'] = 'flash Value'
	 render text: """Country.list
	 <pre>
	 servletContext: ${servletContext.servletContextKey}
	 session: ${session.sessionKey}
	 request: ${request['requestKey']}
	 params: ${params}
	 flash: ${flash}
	 ${link(action:'index', 'Go to index')}
	 </pre>
	 """
	 }*/

	// explicit map instance is returned to view show.gsp
	/*def show() {
	 [country: Country.get(params.long('id'))]
	 // [country: 180]
	 }
	 // Selecting the View
	 def countries() {
	 def map = [ countries: Country.list() ]
	 render(view: "all", model: map)
	 }
	 // Selecting the shared View
	 def palestine() {
	 def map = [ ps: Country.findByCode('PS') ]
	 render(view: "/shared/ps", model: map)*/
	//=====================rendering=========================
	/*def rend() {
	 def countryList = Country.list()
	 // renders text for a specified content-type/encoding
	 //render(text: "<xml>some xml</xml>", contentType: "text/xml", encoding:"UTF-8")
	 // render some markup to the response
	 render {
	 countryList.each { div(id: it?.code, it?.name) }
	 } 
	 // render some XML markup to the response
	 render(contentType: "text/xml") {
	 Countries {
	 for(c in countryList) {
	 Country {
	 code(c?.code?:'missingCode')
	 name(c?.name?:'missingName')
	 }
	 }
	 }
	 }
	 // render a JSON ( http://www.json.org ) response with the builder
	 attribute:
	 render(contentType: "application/json") {
	 array {
	 for(c in countryList) {
	 Country ( code:c?.code?:'missingCode',
	 name:c?.name?:'missingName')
	 }
	 }
	 }
	 // render with status code
	 render(status: 503, text: 'Failed to render')
	 // Automatic marshalling of XML and JSON import grails.converters.*
	 render countryList as JSON
	 //render countryList as XML
	 // render a file
	 render(file: new File('/home/najeh/GrailsCourseS08.pdf'), fileName: "c.pdf",contentType: "application/pdf" )*/
	//========================redirecting=============================================================
	//	def list () {
	//		//def countryList = Country.list()
	//		 redirect(url: "http://grails.org/doc/latest/guide/GORM.html#optimisticLockingAndVersioning")
	//	}
	/*def redi() {
	 // redirects to the list action within the same controller
	 redirect(action: list)
	 // redirects to the index action in the person controller
	 //redirect(controller: 'person', action: 'index')
	 // Can pass parameters
	 redirect(action: 'show', params:[id:170])
	 // Can include a fragment
	 //redirect(controller: "country", action: "list")
	 // Redirect to an explicit URI within the same application
	 //redirect(uri: "/") 
	 // Redirect to a Full URL
	 //redirect(url: "http://www.google.com")
	 }*/
	//============chaning==========================================
	def index() {
		def max = params.long('max')?:150
		def start = params.long('start')?:200
		[countries:Country.list(max: max, offset: start ),max:max,start:start ]



	}
	def show(long id) {
		[country: Country.get(id)]
		//[country: Country.get(params.long('id'))]
	}

	def save () {

		def country = new Country(params)
		if (country.save()) {
			flash.notice = "Country ${country.code}: is saved successfuly"
			redirect(action:'index')
		} else {
			flash.error = "Country ${country.code}: Failed to save"
			render view:'create', model:[country:country]
		}
	}

	def mk () {
		def imageUrl = createLinkTo(dir:"images", file:"grails_logo.png").toString()
		// Prefix the namespace to avoid naming conflicts:
		def imageUrl1 = g.createLinkTo(dir:"images", file:"grails_logo.png").toString()
		render text: "$imageUrl1 \n $imageUrl1"

	}
	def testM(){
		def c = new Country(code: 'CCCC', name:'Farhans country')
		c.save()
		render text:"country has been saved"
	}
}